<template>
  <el-scrollbar wrap-class="scrollbar-wrapper">
    <el-menu
      :show-timeout="200"
      :default-active="$route.path"
      :collapse="isCollapse"
      mode="vertical"
      background-color="#304156"
      text-color="#bfcbd9"
      active-text-color="#409EFF"
    >
      <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>
      <sidebar-item v-for="route in menus" :key="route.path" :item="route" :base-path="route.path"/>
    </el-menu>
  </el-scrollbar>
</template>

<script>
import { mapGetters } from 'vuex'
import router from '@/router'
import store from '@/store'
import SidebarItem from './SidebarItem'
import Hamburger from '@/components/Hamburger'

export default {
  components: { SidebarItem, Hamburger },
  props: {
    menus: {
      type: Array,
      default: function () {
        return []
      },
      required: true
    }
  },
  computed: {
    ...mapGetters([
      'menu_routers',
      'sidebar'
    ]),
    isCollapse () {
      return !this.sidebar.opened
    },
    sidebar () {
      return this.$store.state.app.sidebar
    }
  },
  methods: {
    toggleSideBar () {
      this.$store.dispatch('ToggleSideBar')
    }
  },
  created: function () {
    store.dispatch('GenerateRoutes', this.menus)
    // 动态添加可访问路由表
    router.addRoutes(store.getters.addRouters)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .hamburger-container {
    line-height: 58px;
    height: 50px;
    padding: 0 10px;
  }
</style>
